package cn.iocoder.yudao.module.system.controller.app.notice;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO;
import cn.iocoder.yudao.module.system.controller.app.notice.vo.AppNoticePageReqVO;
import cn.iocoder.yudao.module.system.controller.app.notice.vo.AppNoticeRespVO;
import cn.iocoder.yudao.module.system.controller.app.noticeread.vo.AppNoticeReadSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeDO;
import cn.iocoder.yudao.module.system.dal.dataobject.noticeread.NoticeReadDO;
import cn.iocoder.yudao.module.system.service.notice.NoticeService;
import cn.iocoder.yudao.module.system.service.noticeread.NoticeReadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "App - 通知公告")
@RestController
@RequestMapping("/system/notice")
@Validated
public class AppNoticeController {
    @Resource
    private NoticeService noticeService;
    @Resource
    private NoticeReadService noticeReadService;

    @GetMapping("/page")
    @Operation(summary = "获取通知公告列表")
    @PreAuthenticated
    public CommonResult<PageResult<AppNoticeRespVO>> getNoticePage(@Validated AppNoticePageReqVO pageReqVO) {
        final Long userId = getLoginUserId();
        NoticePageReqVO req = BeanUtils.toBean(pageReqVO, NoticePageReqVO.class);
        req.setStatus(CommonStatusEnum.ENABLE.getStatus());
        PageResult<NoticeDO> pageResult = noticeService.getNoticePage(req);
        PageResult<AppNoticeRespVO> result = BeanUtils.toBean(pageResult, AppNoticeRespVO.class);

        final List<Long> noticeIds = result.getList().stream().map(v -> v.getId()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(noticeIds)) {
            final List<NoticeReadDO> reads = noticeReadService.getNoticeReadByUserIds(noticeIds, userId);
            if (CollUtil.isNotEmpty(reads)) {
                final List<Long> readNoticeIds = reads.stream().map(v -> v.getNoticeId()).collect(Collectors.toList());
                result.getList().forEach(v -> {
                    if (readNoticeIds.contains(v.getId())) {
                        v.setRead(true);
                    }
                });
            }
        }

        return success(result);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通知公告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<AppNoticeRespVO> getNotice(@RequestParam("id") Long id) {
        final Long userId = getLoginUserId();
        final NoticeDO notice = noticeService.getNotice(id);

        final List<NoticeReadDO> reads = noticeReadService.getNoticeReadByUserIds(id, userId);
        if (CollUtil.isEmpty(reads)) {
            final AppNoticeReadSaveReqVO readSaveReqVO = new AppNoticeReadSaveReqVO();
            readSaveReqVO.setNoticeId(id);
            readSaveReqVO.setUserId(userId);
            noticeReadService.createNoticeRead(readSaveReqVO);
        }

        return success(BeanUtils.toBean(notice, AppNoticeRespVO.class));
    }

}
